#include <stdafx.h>
#include <XWidget.h>

XWidget::XWidget(XHash hash, XWindow* window)
{
	Hash = hash;
	Enabled = true;
	Rectangle.X = 0;
	Rectangle.Y = 0;
	Rectangle.W = 100;
	Rectangle.H = 50;
	Render = NULL;
	Text = NULL;
	ParentWindow = window;
}

XWidget::~XWidget()
{
	if(Render != NULL)
		delete Render;
	if(Text != NULL)
		delete Text;
}

void XWidget::_loadVertices()
{
	if(Render != NULL)
	{
		XVertex* vert = new XVertex[4];

		vert[0].X = Rectangle.X;
		vert[0].Y = Rectangle.Y;
		vert[1].X = Rectangle.X + Rectangle.W;
		vert[1].Y = Rectangle.Y;
		vert[2].X = Rectangle.X; 
		vert[2].Y = Rectangle.Y + Rectangle.H;
		vert[3].X = Rectangle.X + Rectangle.W;
		vert[3].Y = Rectangle.Y + Rectangle.H;

		Render->SetVertex(vert, 4);
		delete[] vert;
	}
}

void XWidget::SetEnable()
{
	Enabled = true;

	Render->SetColor(XColor(255, 255, 255, 255));
	Text->SetColor(XColor(200, 200, 200, 255));
}

void XWidget::SetDisable()
{
	Enabled = false;

	Render->SetColor(XColor(255, 255, 255, 100));
	Text->SetColor(XColor(200, 200, 200, 100));
}



void XWidget::SetGeometry(XRectangle rect)
{
	Rectangle = rect;
	_loadVertices();
}

bool XWidget::IsMouseOver(XPosition pos)
{
	if(pos.X > Rectangle.X + Render->GetTranslation().X && pos.X < Rectangle.X + Render->GetTranslation().X + Rectangle.W && pos.Y > Rectangle.Y + Render->GetTranslation().Y && pos.Y < Rectangle.Y + Rectangle.H + Render->GetTranslation().Y)
		return true;
	else
		return false;
}

XRenderPack* XWidget::GetRenderPack()
{
	return Render;
}

XTextPack* XWidget::GetTextPack()
{
	return Text;
}

bool XWidget::GetEnabled()
{
	return Enabled;
}

XRectangle XWidget::GetGeometry()
{
	return Rectangle;
}
